package com.efast.dmom.mgmt.service.mes.report.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.efast.cafe.commponent.query.service.impl.QueryServiceImpl;
import com.efast.cafe.framework.bean.QueryPageBase;
import com.efast.cafe.portal.bean.query.QueryBean;
import com.efast.cafe.portal.bean.query.QueryConditionBean;
import com.efast.cafe.portal.bean.query.QueryOpBean;
import com.efast.dmom.mgmt.bean.mes.report.VehicleCarInformationBean;
import com.efast.dmom.mgmt.dao.mes.report.ReportVehicleRetrospectDao;
import com.efast.dmom.mgmt.entity.report.ReportVehicleRetrospect;
import com.efast.dmom.mgmt.utils.Assist;

/**
 * 重写滞留车通用查询方法
 * @author wj
 *
 */

public class VehicleCarInformationServiceImpl extends QueryServiceImpl{
	
	@Autowired
	private ReportVehicleRetrospectDao reportVehicleRetrospectDao;
	
	@Override
	public Object queryList(QueryBean querys) {
		QueryPageBase result = new QueryPageBase();
		QueryConditionBean queryConditionBean = querys.getQueryConditionBean();
		List<VehicleCarInformationBean> vehicleCarInformationlist = new ArrayList<VehicleCarInformationBean>();
		Assist assit = new Assist();
		Integer count = 0;
		String workShopName = "";
		int start = 0;
		int length = 0;
		if(queryConditionBean!=null) {
			if(queryConditionBean.getStart() == null || queryConditionBean.getLength() == null) {
				queryConditionBean.setStart(0);
				queryConditionBean.setLength(querys.getTemplate().getPagenation());
			}
			assit.setStartRow(queryConditionBean.getStart());
			assit.setRowSize(queryConditionBean.getLength());
			start = assit.getStartRow();
			length = assit.getRowSize();
			List<QueryOpBean> queryOpBeans = queryConditionBean.getQueryOpBeans();
			StringBuilder dynamicRegionSql = new StringBuilder();
			if (queryOpBeans != null && queryOpBeans.size() > 0) {
				for (QueryOpBean data : queryOpBeans) {
					if (data.getKey().equals("workshopNameC") && data.getValue() != null && data.getValue().length() != 0) {
						workShopName = data.getValue();
					} else if (data.getValue() != null && data.getValue().length() != 0
							&& !data.getKey().equals("IFNULL(lot.vin,lot.self_number)")
							&& !data.getKey().equals("workshopNameC")) {
						if (data.getOpName().equals("包含")) {
							assit.setRequires(Assist.andLike(data.getKey(), "%" + data.getValue() + "%"));
						}
						if (data.getOpName().equals("等于")) {
							assit.setRequires(Assist.andEq(data.getKey(), data.getValue()));
						}
						if (data.getOpName() == null || data.getOpName().length() == 0) {
							assit.setRequires(Assist.andEq(data.getKey(), data.getValue()));
						}
					} else if (data.getValue() != null && data.getValue().length() != 0
							&& !data.getKey().equals("workshopNameC")
							&& data.getKey().equals("IFNULL(lot.vin,lot.self_number)")) {
						if (data.getOpName().equals("包含")) {
							// 获取车辆合拼信息
							List<ReportVehicleRetrospect> spliceInfo = reportVehicleRetrospectDao
									.getSpliceInfo(data.getValue());
							dynamicRegionSql.append("lot.vin like '%" +data.getValue()+"%'");
							dynamicRegionSql.append("or lot.self_number like '%"+data.getValue()+"%'");
							if (spliceInfo != null && spliceInfo.size() > 0) {
								for (int j = 0; j < spliceInfo.size(); j++) {
									// 说明为货车合拼之后的查询
									String vinLotName = spliceInfo.get(j).getLotName();
									String selfNumber = spliceInfo.get(j).getSelfNumber();
									dynamicRegionSql.append("or lot.vin like '%" +vinLotName+"%'");
									dynamicRegionSql.append("or lot.self_number like '%"+selfNumber+"%'");
								}
							} 
						}
							if (data.getOpName().equals("等于")) {
								List<ReportVehicleRetrospect> spliceInfo = reportVehicleRetrospectDao
										.getSpliceInfo(data.getValue());
								dynamicRegionSql.append("lot.vin = " +data.getValue());
								dynamicRegionSql.append("or lot.self_number = "+data.getValue());
								List<ReportVehicleRetrospect> spliceInfoByEqual = reportVehicleRetrospectDao
										.getSpliceInfoByEqual(data.getValue());
								if (spliceInfoByEqual != null && spliceInfoByEqual.size() > 0) {
									for (int j = 0; j < spliceInfoByEqual.size(); j++) {
										// 说明为货车合拼之后的查询
										String vinLotName = spliceInfo.get(j).getLotName();
										String selfNumber = spliceInfo.get(j).getSelfNumber();
										dynamicRegionSql.append("or lot.vin = " +vinLotName);
										dynamicRegionSql.append("or lot.self_number = "+selfNumber);
									}
								}
							}
						}
					}
				}
			if (queryOpBeans.size() == 0 || workShopName == null || workShopName.length() == 0) {
				List<VehicleCarInformationBean> weldingList = reportVehicleRetrospectDao.getVehicleCarInformation("BS", assit,dynamicRegionSql.toString());
				int count1 = reportVehicleRetrospectDao.getVehicleCarInformationCount("BS", assit,dynamicRegionSql.toString());
				List<VehicleCarInformationBean> coatingList = reportVehicleRetrospectDao.getVehicleCarInformation("PS", assit,dynamicRegionSql.toString());
				int count2 = reportVehicleRetrospectDao.getVehicleCarInformationCount("PS", assit,dynamicRegionSql.toString());
				List<VehicleCarInformationBean> finalList = reportVehicleRetrospectDao.getVehicleCarInformation("AS", assit,dynamicRegionSql.toString());
				int count3 = reportVehicleRetrospectDao.getVehicleCarInformationCount("AS", assit,dynamicRegionSql.toString());
				count = count1 + count2 + count3;
				if (weldingList != null && weldingList.size() > 0) {
					for (VehicleCarInformationBean data : weldingList) {
						vehicleCarInformationlist.add(data);
					}
				}
				if (coatingList != null && coatingList.size() > 0) {
					for (VehicleCarInformationBean data : coatingList) {
						vehicleCarInformationlist.add(data);
					}
				}
				if (finalList != null && finalList.size() > 0) {
					for (VehicleCarInformationBean data : finalList) {
						vehicleCarInformationlist.add(data);
					}
				}
			} else if (queryOpBeans.size() > 0 && workShopName != null) {
				List<VehicleCarInformationBean> list = reportVehicleRetrospectDao.getVehicleCarInformation(workShopName, assit,dynamicRegionSql.toString());
				count = reportVehicleRetrospectDao.getVehicleCarInformationCount(workShopName, assit,dynamicRegionSql.toString());
				if (list != null && list.size() > 0) {
					for (VehicleCarInformationBean data : list) {
						vehicleCarInformationlist.add(data);
					}
				}
			}
		}
		//滞留车时间排除非工作日
		if(vehicleCarInformationlist != null && vehicleCarInformationlist.size() >0) {
		this.ListSort(vehicleCarInformationlist);
		List<VehicleCarInformationBean> vehicleCarInformationSizelist = new ArrayList<VehicleCarInformationBean>();
		if(length != 0 && (length+start) < count) {
			for(int i=start;i<length+start;i++) {	
				vehicleCarInformationSizelist.add(vehicleCarInformationlist.get(i));
			}
		} else if(length != 0 && (length+start) > count) {
			for(int i=start;i<count;i++) {
				vehicleCarInformationSizelist.add(vehicleCarInformationlist.get(i));
			}
		} else {
			for (VehicleCarInformationBean data:vehicleCarInformationlist) {
				vehicleCarInformationSizelist.add(data);
			}
		}
		result.setResultList(vehicleCarInformationSizelist);
		result.setTotalRows(count);
		}
		return result;
	}

	 //时间排序算法
	private void ListSort(List<VehicleCarInformationBean> list) {
	    Collections.sort(list, new Comparator<VehicleCarInformationBean>() {
	        @Override
	        public int compare(VehicleCarInformationBean o1, VehicleCarInformationBean o2) {
	            try {
	                Date dt1 = o1.getCompletedTime();
	                Date dt2 = o2.getCompletedTime();
	                if (dt1.getTime() < dt2.getTime()) {
	                    return 1;
	                } else if (dt1.getTime() > dt2.getTime()) {
	                    return -1;
	                } else {
	                    return 0;
	                }
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	            return 0;
	        }
	    });
	}
}
